<template>
  <el-dialog
    :title="!dataForm.pid ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="站点名称" prop="pName">
      <el-input v-model="dataForm.pName" placeholder="站点名称"></el-input>
    </el-form-item>
    <el-form-item label="地址" prop="pAddress">
      <el-input v-model="dataForm.pAddress" placeholder="地址"></el-input>
    </el-form-item>
    <el-form-item label="服务电话" prop="pPhone">
      <el-input v-model="dataForm.pPhone" placeholder="服务电话"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="pCreateTime">
      <el-input v-model="dataForm.pCreateTime" placeholder="创建时间(格式：2022-02-02 00:00:00)"></el-input>
    </el-form-item>
    <el-form-item label="更新时间" prop="pUpdateTime">
      <el-input v-model="dataForm.pUpdateTime" placeholder="更新时间(格式：2022-02-02 00:00:00)"></el-input>
    </el-form-item>
    <el-form-item label="备注" prop="pComment">
      <el-input v-model="dataForm.pComment" placeholder="备注"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          pid: 0,
          pName: '',
          pAddress: '',
          pPhone: '',
          pCreateTime: '',
          pUpdateTime: '',
          pComment: ''
        },
        dataRule: {
          pName: [
            { required: true, message: '站点名称不能为空', trigger: 'blur' }
          ],
          pAddress: [
            { required: true, message: '地址不能为空', trigger: 'blur' }
          ],
          pPhone: [
            { required: true, message: '服务电话不能为空', trigger: 'blur' }
          ],
          pCreateTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          pUpdateTime: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ],
          pComment: [
            { required: true, message: '备注不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.pid = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.pid) {
            this.$http({
              url: this.$http.adornUrl(`/bike/place/info/${this.dataForm.pid}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.pName = data.place.pname
                this.dataForm.pAddress = data.place.paddress
                this.dataForm.pPhone = data.place.pphone
                this.dataForm.pCreateTime = data.place.pcreateTime
                this.dataForm.pUpdateTime = data.place.pupdateTime
                this.dataForm.pComment = data.place.pcomment
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/bike/place/${!this.dataForm.pid ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'pid': this.dataForm.pid || undefined,
                'pname': this.dataForm.pName,
                'paddress': this.dataForm.pAddress,
                'pphone': this.dataForm.pPhone,
                'pcreateTime': this.dataForm.pCreateTime,
                'pupdateTime': this.dataForm.pUpdateTime,
                'pcomment': this.dataForm.pComment
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
